package mobi.hami.huapin.service.impl;

import mobi.hami.huapin.dao.SkuAttributeMapper;
import mobi.hami.huapin.dto.PageDTO;
import mobi.hami.huapin.model.SkuAttribute;
import mobi.hami.huapin.service.SkuAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Service
public class SkuAttributeServiceImpl implements SkuAttributeService {
    @Autowired
    private SkuAttributeMapper skuAttributeMapper;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public List<SkuAttribute> getAttributeByProductid(Integer Productid) {
        return skuAttributeMapper.getAttributeByProductid(Productid);
    }

    @Override
    public int add(SkuAttribute attribute) {
        return skuAttributeMapper.insert(attribute);
    }

    @Override
    public PageDTO<SkuAttribute> getListPage(PageDTO page) {
        PageDTO<SkuAttribute> pageDTO = new PageDTO<SkuAttribute>();
        String sql = "SELECT COUNT(1) from  sku_attribute where  " + page.getStr();
        int rs = jdbcTemplate.queryForObject(sql, null, Integer.class);
        String sqlstr = "SELECT * from  sku_attribute where  " + page.getStr() + "  order by " + page.getOrder() + " desc  limit "
                + page.getPageindex() + "," + page.getPagesize();
        List<SkuAttribute> users = jdbcTemplate.query(sqlstr, new RowMapper<SkuAttribute>() {
            @Override
            public SkuAttribute mapRow(ResultSet rs, int rowNum) throws SQLException {
                SkuAttribute user = new SkuAttribute();
                user.setGmt_create(rs.getTime("gmt_create"));
                user.setName(rs.getString("name"));
                user.setDescription(rs.getString("description"));
                user.setGmt_modified(rs.getTime("gmt_modified"));
                user.setProductid(rs.getInt("productid"));
                user.setId(rs.getInt("id"));
                return user;
            }
        });
        pageDTO.setData(users);
        pageDTO.setTotal(rs);
        return pageDTO;
    }
    @Override
    public int delteAttributeIds(int id) {
        return skuAttributeMapper.deleteByPrimaryKey(id);
    }

  public   int Update(SkuAttribute attribute){
      return skuAttributeMapper.updateByPrimaryKey(attribute);
  }
}
